package com.tyj.movie.service.impl;

import com.tyj.movie.config.interceptor.CityInterceptor;
import com.tyj.movie.entity.*;
import com.tyj.movie.mapper.*;
import com.tyj.movie.service.AdministrativeRegionService;
import com.tyj.movie.service.MovieTheatreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyj.movie.util.DateUtil;
import com.tyj.movie.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Service
@Slf4j
public class MovieTheatreServiceImpl extends ServiceImpl<MovieTheatreMapper, MovieTheatre> implements MovieTheatreService {

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    MovieTheatreServerMapper movieTheatreServerMapper;

    @Autowired
    AdministrativeRegionMapper administrativeRegionMapper;

    @Autowired
    MovieTheatreBrandMapper movieTheatreBrandMapper;

    @Autowired
    MovieTheatreShowMapper movieTheatreShowMapper;

    @Autowired
    MovieMapper movieMapper;

    @Autowired
    MovieTypeMapper movieTypeMapper;

    @Autowired
    MovieActorMapper movieActorMapper;

    /**
     * 查询影院页信息
     *
     * @return
     */
    @Override
    public CinemasVo queryMovieTheatreVo() throws ExecutionException, InterruptedException {

        CinemasVo movieTheatreVo = new CinemasVo();

        //获取当前城市
        City city = CityInterceptor.threadLocal.get();

        //查询影院数据
        CompletableFuture<List<MovieTheatreVo>> voidCompletableFuture1 = CompletableFuture.supplyAsync(() -> {
            List<MovieTheatreVo> list = this.baseMapper.queryMovieTheatre(city.getId());
            //获取标签
            for (MovieTheatreVo theatreVo : list) {
                List<String> labels = movieTheatreServerMapper.queryLabelByMovieTheatreId(theatreVo.getId());
                theatreVo.setSeverLabels(labels);
            }
            movieTheatreVo.setMovieTheatre(list);
            log.debug("任务一完毕{}", list);
            return list;
        }, threadPoolExecutor);

        //查询电影品牌
        CompletableFuture<Void> voidCompletableFuture2 = voidCompletableFuture1.thenAcceptAsync((list) -> {
            List<MovieTheatreBrand> list1 = movieTheatreBrandMapper.queryBrandByList(list);
            movieTheatreVo.setMovieTheatreBrandList(list1);
            log.debug("任务二完毕{}", list1);

        }, threadPoolExecutor);

        //查询电影行政区
        CompletableFuture<Void> voidCompletableFuture3 = CompletableFuture.runAsync(() -> {
            List<AdministrativeRegion> list2 = administrativeRegionMapper.queryAdministrativeRegionByCityId(city.getId());
            movieTheatreVo.setMovieTheatreAdministrativeRegionList(list2);
            log.debug("任务三完毕{}", list2);

        }, threadPoolExecutor);


        CompletableFuture.allOf(voidCompletableFuture1, voidCompletableFuture2, voidCompletableFuture3).get();

        return movieTheatreVo;
    }

    @Override
    public List<CinemasDetailShowVo> queryCinemasDetailShowVo(Long movieTheatreId) throws ParseException {
        //封装的最终数据
        List<CinemasDetailShowVo> cinemasDetailShowVoList = new ArrayList<>();

        //查询需要放映的电影信息
        List<CinemasMovieVo> cinemasMovieVos = movieTheatreShowMapper.queryNotShowMovieBymovieTheatreId(movieTheatreId);

        //查询所有放映信息
        List<MovieTheatreShow> movieTheatreShows = movieTheatreShowMapper.queryNotShowMovieShowBymovieTheatreId(movieTheatreId);

        //开始封装数据
        for (CinemasMovieVo cinemasMovieVo : cinemasMovieVos) {
            CinemasDetailShowVo detailShowVo = new CinemasDetailShowVo();

            //封装电影数据
            //查询主演演员名
            List<MovieActor> movieActors = movieActorMapper.queryActorNotNullByMovieId(cinemasMovieVo.getId());
            List<String> movieActorNames = movieActors.stream().map((movieActor) -> {

                return movieActor.getName();
            }).collect(Collectors.toList());
            cinemasMovieVo.setTypes(movieActorNames);

            //查询类型名
            List<String> movieTypes = movieTypeMapper.queryMovieTypesByMovieId(cinemasMovieVo.getId());
            cinemasMovieVo.setActorNames(movieTypes);
            detailShowVo.setCinemasMovieVo(cinemasMovieVo);

            //封装影院排片数据
            Map<Long, List<MovieTheatreShow>> movieTheatreShowMap = new TreeMap<>();
            for (MovieTheatreShow movieTheatreShow : movieTheatreShows) {
                //将日期转换为 yyyy-MM-dd
                Date startDate = movieTheatreShow.getStartDate();
                Calendar instance = Calendar.getInstance();
                instance.setTime(startDate);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                int day = instance.get(Calendar.DATE);//一年中的第几天
                int month = instance.get(Calendar.MONTH) + 1;//第几个月
                int year = instance.get(Calendar.YEAR);//年份数值
                //转换成功
                Date parse = simpleDateFormat.parse(year + "-" + month + "-" + day);
                long time = parse.getTime();
                //判断是否是当前电影
                if (movieTheatreShow.getMovieId().equals(cinemasMovieVo.getId())) {
                    if (movieTheatreShowMap.containsKey(time)) {
                        //存在key
                        List<MovieTheatreShow> movieTheatreShows1 = movieTheatreShowMap.get(time);
                        movieTheatreShows1.add(movieTheatreShow);
                    } else {
                        //不存在key
                        List<MovieTheatreShow> arrayList = new ArrayList<>();
                        arrayList.add(movieTheatreShow);
                        movieTheatreShowMap.put(time, arrayList);
                    }
                }
            }

            //排序集合
            for (Long aLong : movieTheatreShowMap.keySet()) {
                List<MovieTheatreShow> movieTheatreShows1 = movieTheatreShowMap.get(aLong);
                Collections.sort(movieTheatreShows1,(o1, o2) ->{
                 return (int)(o1.getStartDate().getTime()-o2.getStartDate().getTime());
                });
            }

            detailShowVo.setMovieTheatreShows(movieTheatreShowMap);

            //封装日期数据
            List<String> riqi = new ArrayList<>();
            for (Long aLong : movieTheatreShowMap.keySet()) {
                Date date = new Date();
                date.setTime(aLong);
                String str = "";
                if (DateUtil.getDayString(date) == 0) {
                    str = "今天";
                } else if (DateUtil.getDayString(date) == 1) {
                    str = "明天";
                } else if (DateUtil.getDayString(date) == 2) {
                    str = "后天";
                } else if (DateUtil.getDayString(date) >= 3) {
                    str = DateUtil.getWeek(date);
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM月dd日");
                String format = simpleDateFormat.format(date);

                riqi.add(str + " " + format);
            }
            detailShowVo.setDateShow(riqi);
            cinemasDetailShowVoList.add(detailShowVo);
        }


        return cinemasDetailShowVoList;
    }

    @Override
    public MovieTheatre queryMovieTheatreAndBrand(Long movieTheatreId) {
        MovieTheatre movieTheatre = this.baseMapper.queryMovieTheatreAndBrand(movieTheatreId);
        return movieTheatre;
    }


}
